//
//  EveryDayModel.m
//  project
//
//  Created by 黄建华 on 2017/9/26.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "EveryDayModel.h"
#define HHHEveryDayModel @"HHHEveryDayModel"

//**************************************************用户模型**********************************************
@implementation EveryDayModel

- (instancetype)init {
    self = [super init];
    if (self) {
    }
    
    return self;
}

+ (EveryDayModel *)simpleInitWithDate:(NSDate *)date {
    EveryDayModel *model = [[EveryDayModel alloc] init];
    
    //计步初始
    model.dateString = [[date dateToString] componentsSeparatedByString:@" "][0];
    model.user_id    = [KK_LastUser getObjectValue];
    model.saveData   = 0;
    model.weekCount  = [NSDate getDaysWeekNumberWithSelectDate:date];
    model.monthCount = [NSDate getDaysMonthNumberWithSelectDate:date];
    model.dayOfWeek  = [NSDate getDaysYearNumberWithSelectDate:date withType:0];
    model.dayOfMonth = [NSDate getDaysYearNumberWithSelectDate:date withType:1];
    model.yearCount  = [NSDate getDaysYearNumberWithSelectDate:date withType:2];
    
    return model;
}

// 表名
+ (NSString *)getTableName {
    return @"EveryDayModelTable";
}

// 复合主键 1 用户账号 2 日期
+ (NSArray *)getPrimaryKeyUnionArray {
    return @[@"user_id", @"dateString"];
}

// 选择日期是选择日期年的第几周
+ (NSInteger)getDaysWeekNumberWithSelectDate:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger  week      = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitYear forDate:date];
    
    //    NSLog(@"选择日期是选择日期年的第%ld周",week);
    return week;
}

// 选择日期是选择日期年的第几月
+ (NSInteger)getDaysMonthNumberWithSelectDate:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger  month     = [calendar ordinalityOfUnit:NSCalendarUnitMonth inUnit:NSCalendarUnitYear forDate:date];
    
    //    NSLog(@"选择日期是选择日期年的第%ld月",month);
    return month;
}

//选择日期是选择日期年的年份
+ (NSInteger)getDaysYearNumberWithSelectDate:(NSDate *)date withType:(NSInteger)type {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    unsigned   unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *components = [calendar components:unitFlags fromDate:date];
    
    //    NSLog(@"选择日期的年份是>>>>%d",components.year);
    if (type == 0) {
        return components.weekday;
    } else if (type == 1) {
        return components.day;
    } else {
        return components.year;
    }
}

+ (void)initialize {
    [super initialize];
}

@end

